import urllib.request
from bs4 import BeautifulSoup


#爬取某个游戏的详细信息 将其定义为一个方法 从另一个方法当中进行调用
def spider_detail_info(url):
    req = urllib.request.Request(url)
    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36')
    content = urllib.request.urlopen(req).read().decode('gbk')
    #使用BeautifulSoup进行解析
    soup = BeautifulSoup(content,features='lxml')
    return soup
#爬取游戏的介绍
def spider_introduce(soup,game_intro):
    soup = soup.find('div', {'class': 'info'})
    # print(soup)
    if soup is not None:
        soup = soup.find('div', {'id': 'introduce'})
        # find寻找的应该是子标签  下面这种方式填写是不正确的soup.find('div').find(text=True).strip()
        # introduce = soup.find(text=True).strip()
        # print(introduce)
        # 把介绍的具体内容爬取出来 此处需要加一些判断，因为有些游戏没有介绍，同时有些游戏在front标签下面没有strong标签
        if soup is not None:
            intro_content = soup.find('font').find(text=True).strip()
            # print(intro_content)
            game_intro.append(intro_content)
            # print(soup)
        else:
            game_intro.append("本页没有该游戏的内容介绍")
    else:
        game_intro.append("本页没有该游戏的内容介绍")
        # print(dictionary)
    return game_intro
# if __name__ == '__main__':
#     url = 'http://www.4399.com/flash/55021.htm'
#     soup = spider_detail_info(url)
#     print(spider_introduce(soup))